package springcloud.controller;

import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.nyist.springcloud.ResultVO;
import com.nyist.springcloud.entities.Payment;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springcloud.service.FeignService;


/**
 * @author yangxiaogen
 * @date 2021/1/31 14:36
 */
@DefaultProperties(defaultFallback = "paymentInfo_TimeOutHandlerDefault")
@RestController
@RequestMapping("/consumer/hystrix")
@Slf4j
public class FeignController {

    @Autowired
    private FeignService feignService;

    @PostMapping(value = "/pay/ok")
    public ResultVO<Payment> getPaymentOk(@RequestBody Payment payment) {
        //int i = 10/0;
        return feignService.getPaymentOk(payment);
    }

    /*@HystrixCommand(fallbackMethod = "paymentInfo_TimeOutHandler",
            commandProperties = {
                    @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1500")  //3秒钟以内就是正常的业务逻辑
            })*/
    @PostMapping(value = "/pay/timeout")
    public ResultVO<Payment> getPaymentTimeout(@RequestBody Payment payment) {
        // int i = 10/0;
        return feignService.getPaymentTimeout(payment);
    }

    public ResultVO<Payment> paymentInfo_TimeOutHandler(@RequestBody Payment payment) {

        ResultVO<Payment> paymentResultVO = new ResultVO<>();

        paymentResultVO.setCode("400");
        paymentResultVO.setMessage("超时fallback....");

        return paymentResultVO;
    }

    public ResultVO<Payment> paymentInfo_TimeOutHandlerDefault(@RequestBody Payment payment) {

        ResultVO<Payment> paymentResultVO = new ResultVO<>();

        paymentResultVO.setCode("500");
        paymentResultVO.setMessage("Default ----- fallback....");

        return paymentResultVO;
    }


}